home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Utilities Professional 1-1500
/
Utilities Professional 1-1500 (1994)(WPD)[!].iso
/
12511500
/
var1451.dms
/
var1451.adf
/
ParallelDevice
/
Example4.c
< prev
next >
Wrap
C/C++ Source or Header
|
1992-04-27
|
23KB
|
562 lines
/***********************************************************/
/* */
/* Amiga C Encyclopedia (ACE) V3.0 Amiga C Club (ACC) */
/* ------------------------------- ------------------ */
/* */
/* Book: ACM Devices Amiga C Club */
/* Chapter: Parallel Device Tulevagen 22 */
/* File: Example4.c 181 41 LIDINGO */
/* Author: Anders Bjerin SWEDEN */
/* Date: 92-04-26 */
/* Version: 1.00 */
/* */
/* Copyright 1992, Anders Bjerin - Amiga C Club (ACC) */
/* */
/* Registered members may use this program freely in their */
/* own commercial/noncommercial programs/articles. */
/* */
/***********************************************************/
/* This example does not do anything, but it consists of */
/* several useful functions that you can use yourself after */
/* small modifications. The functions demonstrates all */
/* commands there exist for the parallel device, so if you */
/* had problems in understanding how a command was used you */
/* can look here. */
#include <exec/types.h>
#include <exec/errors.h>
#include <devices/parallel.h>
/* Declare the functions: */
/* Sets the parallel parameters: */
UBYTE SetParParams(
struct IOExtPar *ioreq,
UBYTE parallel_flags,
ULONG extended_flags,
UBYTE *eof_chars
);
/* Explains error messages: */
void ParError( UBYTE error );
/* Sends data to the parallel device: */
UBYTE ParWrite(
struct IOExtPar *ioreq,
BYTE *data,
ULONG length
);
/* Collects data from the parallel device: */
UBYTE ParRead(
struct IOExtPar *ioreq,
BYTE *data,
ULONG length
);
/* Sends data to the parallel device without waiting: */
void ParWriteNoWait(
struct IOExtPar *ioreq,
BYTE *data,
ULONG length
);
/* Collects data from the parallel device without waiting: */
void ParReadNoWait(
struct IOExtPar *ioreq,
BYTE *data,
ULONG length
);
/* Remove all queued requests: */
UBYTE ParFlush( struct IOExtPar *ioreq );
/* Print some information about the parallel device: */
UBYTE ParQuery( struct IOExtPar *ioreq );
/* Reset the parallel device: */
UBYTE ParReset( struct IOExtPar *ioreq );
/* Temporarily stop all paralllel communication: */
UBYTE ParStop( struct IOExtPar *ioreq );
/* Restart parallel communication: */
UBYTE ParStart( struct IOExtPar *ioreq );
/* Do hardly nothing: */
void main();
void main()
{
printf( "This example consists of many support functions,\n" );
printf( "but does not do anything. See source code...\n" );
}
/***************************************/
/* PARALLEL DEVICE - SUPPORT FUNCTIONS */
/***************************************/
/* SetParParams() sets the parallel parameters. It initializes a IOExtPar */
/* structure, and does a PDCMD_SETPARAMS commad. If everything is OK it */
/* returns NULL, else an error number is returned. */
/* */
/* Synopsis: er = SetParParams( io, bl, br, bt, rl, wl, sl, sf, ef, chr ); */
/* */
/* er: (UBYTE) SetParParams() returns 0 if everything was OK, else */
/* an error value is returned. See function ParError() for more */
/* information. */
/* */
/* io: (struct IOExtPar *) Pointer to the parallel request block you */
/* want to initialize. */
/* */
/* PARF_RAD_BOOGIE Not supported by the parallel device for the */
/* moment. */
/* */
/* PARF_SHARED Set this flag if you want to allow other */
/* tasks running at the same time to use the */
/* parallel device. The default is exclusive- */
/* access. (If some other task is using the */
/* parallel device with the shared bit set, and */
/* you call this function with exclusive access, */
/* your request will fail.) */
/* */
/* PARF_EOFMODE Set this flag if you want to check for end of */
/* file characters. (You may use up to eight end */
/* of file characters, which are specified */
/* below.) */
/* */
/* ef: (ULONG) Not supported by the parallel device for the moment. */
/* */
/* chr: (UBYTE *) Pointer to an array containing eight end-of-file */
/* characters. If the parallel flag "PARF_EOFMODE" is set, the */
/* parallel device will check each character which is sent or */
/* received, and if it matches one of the end-of-file characters */
/* the read/wite request is terminated. */
UBYTE SetParParams(
struct IOExtPar *ioreq, /* Pointer to our parallel request block. */
UBYTE parallel_flags, /* Parallel flags. */
ULONG extended_flags, /* Additional parallel flags. */
UBYTE *eof_chars /* Pointer to an array containing eight end-of- */
/* file characters. */
)
{
int loop; /* Used in the loop. */
UBYTE *ptr; /* Unsigned byte pointer. */
/* Set parallel flags: */
ioreq->io_ParFlags = parallel_flags;
/* Set additional flags: */
ioreq->io_PExtFlags = extended_flags;
/* Get the address of the IOTArray: */
ptr = (UBYTE *) &(ioreq->io_PTermArray);
/* Set all eight end of file characters: */
for( loop=0; loop < 8; loop++ )
{
/* Copy character after character: */
*ptr = eof_chars[ loop ];
/* Step one byte foreward: */
ptr++;
}
/* All values have now been set, lets do a PDCMD_SETPARAMS request: */
ioreq->IOPar.io_Command = PDCMD_SETPARAMS;
/* Do our request, and when complete return 0 if */
/* OK, else an error value: */
return( (UBYTE) DoIO( ioreq ) );
}
/* ParError() tells the user what went wrong. You give it the error code */
/* you received, and ParError() will print a short description of the */
/* problem. Useful when debugging. */
/* */
/* Synopsis: ParError( error ); */
/* */
/* error: (UBYTE) The error value you want to have explained. */
void ParError( UBYTE error )
{
switch( error )
{
/* Parallel device errors: */
case ParErr_DevBusy:
printf( "Some other task is already using the parallel Device!\n" );
break;
case ParErr_BufTooBig:
printf( "The parallel buffer is too big! (?)\n" );
break;
case ParErr_InvParam:
printf( "Invalid parameters!\n" );
break;
case ParErr_LineErr:
printf( "Line error, check all cables!\n" );
break;
case ParErr_NotOpen:
printf( "The Parallel Device is not open!\n" );
break;
case ParErr_PortReset:
printf( "Someone has resetted the Parallel Device!\n" );
break;
case ParErr_InitErr:
printf( "Could not initialize the Parallel Device!\n" );
break;
/* Exec errors: */
case IOERR_OPENFAIL:
printf( "The device could not be opened!\n" );
break;
case IOERR_ABORTED:
printf( "The request was aborted!\n" );
break;
case IOERR_NOCMD:
printf( "The parallel device does not know about this command!\n" );
break;
case IOERR_BADLENGTH:
printf( "The length of the request was not valid!\n" );
break;
/* Unknown error: */
default:
printf( "Unknown error! Error code: %d\n", error );
}
}
/* ParWrite() sends some data to the Parallel Port. You only have */
/* to give it a pointer to the data you want to write and tell it */
/* how many bytes you want to transfer. */
/* */
/* Synopsis: error = ParWrite( io, data, length ); */
/* */
/* error: (UBYTE) ParWrite() returns 0 if everything was OK, */
/* else an error number is returned. */
/* */
/* io: (struct IOExtPar *) Pointer to an initialized */
/* parallel request block. */
/* */
/* data: (BYTE *) Pointer to the first byte of the data you */
/* want to send (write). */
/* */
/* length: (ULONG) How many bytes you want to transfer. If you */
/* want to continue to send data until we have received */
/* an end-of-file character, set the length to -1. */
/* (Note that the parallel device will then ONLY stop */
/* when it has received one of the end-of-file */
/* characters.) */
UBYTE ParWrite(
struct IOExtPar *ioreq, /* Pointer to our parallel request block. */
BYTE *data, /* Pointer to the data you want to send. */
ULONG length /* The length of the data you want to send. */
)
{
/* We want to send (write) some data: */
ioreq->IOPar.io_Command = CMD_WRITE;
/* Give the start address of our data: */
ioreq->IOPar.io_Data = (APTR) data;
/* Set the length of the message: (If you want to continue */
/* to write until you have received an end-of-file character, */
/* set length to -1.) */
ioreq->IOPar.io_Length = length;
/* Do our request, and return 0 if everything is OK, else */
/* return an error number: (This is a task sleep.) */
return( (UBYTE) DoIO( ioreq ) );
}
/* ParRead() reads some data from the Parallel Port. You only have */
/* to give it a pointer to some memory where the data should be */
/* stored, and tell it how many bytes you want to read. The rest is */
/* done automatically. */
/* */
/* Synopsis: error = ParRead( io, data, length ); */
/* */
/* error: (UBYTE) ParRead() returns 0 if everything was OK, else */
/* an error number is returned. */
/* */
/* io: (struct IOExtPar *) Pointer to an initialized parallel */
/* request block. */
/* */
/* data: (BYTE *) Pointer to the memory buffer where you want */
/* to store all collected data. */
/* */
/* length: (ULONG) How many bytes you want to read. If you want to */
/* continue to read data until we have received an end-of- */
/* file character, set the length to -1. */
UBYTE ParRead(
struct IOExtPar *ioreq, /* Pointer to our parallel request block. */
BYTE *data, /* Where the data should be placed. */
ULONG length /* How many bytes you want to read. */
)
{
/* We want to read some data: */
ioreq->IOPar.io_Command = CMD_READ;
/* Give the start address of our data: */
ioreq->IOPar.io_Data = (APTR) data;
/* Set how many bytes you want to read. (If you want to continue */
/* to read data until you have received an end-of-file character, */
/* set length to -1.) */
ioreq->IOPar.io_Length = length;
/* Do our request, and return 0 if everything is OK, else */
/* return an error number: (This is a task sleep. Zzz ) */
return( (UBYTE) DoIO( ioreq ) );
}
/* ParWriteNoWait() sends some data to the Parallel Port, but returns */
/* immediately. You only have to give it a pointer to the data you */
/* want to write and tell it how many bytes you want to transfer. */
/* Since it does not wait for the request to be completed, you have */
/* to take care of removing the message yourself. Note that all */
/* requests that have been started must be completed or aborted */
/* before your program may close the parallel device. */
/* */
/* Synopsis: error = ParWriteNoWait( io, data, length ); */
/* */
/* io: (struct IOExtPar *) Pointer to an initialized parallel */
/* request block. */
/* */
/* data: (BYTE *) Pointer to the first byte of the data you want */
/* to send (write). */
/* */
/* length: (ULONG) How many bytes you want to transfer. If you want */
/* to continue to send data until we have received an end- */
/* of-file character, set the length to -1. (Note that the */
/* parallel device will then ONLY stop when it receives one */
/* of the specified end-of-file characters.) */
void ParWriteNoWait(
struct IOExtPar *ioreq, /* Pointer to our parallel request block. */
BYTE *data, /* Pointer to the data you want to send. */
ULONG length /* The length of the data you want to send. */
)
{
/* We want to send (write) some data: */
ioreq->IOPar.io_Command = CMD_WRITE;
/* Give the start address of our data: */
ioreq->IOPar.io_Data = (APTR) data;
/* Set the length of the message: */
ioreq->IOPar.io_Length = length;
/* Do our request and return immediately: */
SendIO( ioreq );
}
/* ParReadNoWait() reads some data from the Parallel Port, but returns */
/* immediately. You only have to give it a pointer to some memory */
/* where the data should be stored, and tell it how many bytes you */
/* want to read. Since it does not wait for the request to be */
/* completed, you have to take care of removing the message yourself. */
/* Note that all requests that have been started must be completed or */
/* aborted before your program may close the parallel device. */
/* */
/* */
/* Synopsis: error = ParReadNoWait( io, data, length ); */
/* */
/* io: (struct IOExtPar *) Pointer to an initialized parallel */
/* request block. */
/* */
/* data: (BYTE *) Pointer to the memory buffer where you want to */
/* store all data. */
/* */
/* length: (ULONG) How many bytes you want to read. If you want to */
/* continue to send data until we have received an end-of- */
/* file character, set the length to -1. (Note that the */
/* parallel device will then ONLY stop when it receives one */
/* of the end-of-file characters.) */
void ParReadNoWait(
struct IOExtPar *ioreq, /* Pointer to our parallel request block. */
BYTE *data, /* Where the data should be placed. */
ULONG length /* How many bytes you want to read. */
)
{
/* We want to read some data: */
ioreq->IOPar.io_Command = CMD_READ;
/* Give the start address of our data: */
ioreq->IOPar.io_Data = (APTR) data;
/* Set how many bytes you want to read: */
ioreq->IOPar.io_Length = length;
/* Do our request and return immediately: */
DoIO( ioreq );
}
/* ParFlush() will remove all queued commands. */
/* */
/* Synopsis: error = ParFlush( io ); */
/* */
/* error: (UBYTE) The function returns 0 if everything was */
/* OK, else an error number is returned. */
/* */
/* io: (struct IOExtPar *) Pointer to an initialized */
/* parallel request block. */
UBYTE ParFlush( struct IOExtPar *ioreq )
{
/* We want to remove all queued requests: */
ioreq->IOPar.io_Command = CMD_FLUSH;
/* Do our request, and return 0 if everything is OK, else */
/* return an error number: */
return( (UBYTE) DoIO( ioreq ) );
}
/* ParQuery() will print some information about the parallel */
/* device. This is very useful for debugging. */
/* */
/* Synopsis: error = ParQuery( io ); */
/* */
/* error: (UBYTE) The function returns 0 if everything was */
/* OK, else an error number is returned. */
/* */
/* io: (struct IOExtPar *) Pointer to an initialized */
/* parallel request block. */
UBYTE ParQuery( struct IOExtPar *ioreq )
{
UBYTE error;
/* Check the parallel device: */
ioreq->IOPar.io_Command = PDCMD_QUERY;
/* Do our request: */
error = DoIO( ioreq );
/* OK? */
if( error )
printf( "Could not get any information from the device!\n" );
else
{
/* Check the "io_Status" field: */
if( ioreq->io_Status & IOPTF_PARBUSY )
printf( "Printer is busy.\n" );
if( ioreq->io_Status & IOPTF_PAPEROUT )
printf( "Paper out!\n" );
if( ioreq->io_Status & IOPTF_PARSEL )
printf( "Printerr selected!\n" );
printf( "Device is %s\n",
ioreq->io_Status & IOPTF_RWDIR ? "Writing" : "Reading" );
}
return( error );
}
/* ParReset() will reset the parallel device. All commands that */
/* are qued to the device will be removed, commands that are */
/* currently executed will be aborted and finally all parallel */
/* flags are resetted. */
/* */
/* Synopsis: error = ParReset( io ); */
/* */
/* error: (UBYTE) The function returns 0 if everything was */
/* OK, else an error number is returned. */
/* */
/* io: (struct IOExtPar *) Pointer to an initialized */
/* parallel request block. */
UBYTE ParReset( struct IOExtPar *ioreq )
{
/* We want to reset the parallel device: */
ioreq->IOPar.io_Command = CMD_RESET;
/* Do our request, and return 0 if everything is OK, else */
/* return an error number: */
return( (UBYTE) DoIO( ioreq ) );
}
/* ParStop() will temporary stop the parallel communication. */
/* */
/* Synopsis: error = ParStop( io ); */
/* */
/* error: (UBYTE) The function returns 0 if everything was */
/* OK, else an error number is returned. */
/* */
/* io: (struct IOExtPar *) Pointer to an initialized */
/* parallel request block. */
UBYTE ParStop( struct IOExtPar *ioreq )
{
/* We want to start parallel communication again: */
ioreq->IOPar.io_Command = CMD_STOP;
/* Do our request, and return 0 if everything is OK, else */
/* return an error number: */
return( (UBYTE) DoIO( ioreq ) );
}
/* ParStart() will restart the parallel communication, which */
/* has previously been halted by a ParStop() call. */
/* */
/* Synopsis: error = ParStart( io ); */
/* */
/* error: (UBYTE) The function returns 0 if everything was */
/* OK, else an error number is returned. */
/* */
/* io: (struct IOExtPar *) Pointer to an initialized */
/* parallel request block. */
UBYTE ParStart( struct IOExtPar *ioreq )
{
/* We want to start parallel communication again: */
ioreq->IOPar.io_Command = CMD_START;
/* Do our request, and return 0 if everything is OK, else */
/* return an error number: */
return( (UBYTE) DoIO( ioreq ) );
}